#include "LoadInstruction.h"
#include <iostream>

LoadInstruction::LoadInstruction(Function * func, Value * result, Value * ptr)
    : Instruction(func, IRInstOperator::IRINST_OP_LOAD, result->getType()), ptr_(ptr), result_(result)
{
    // 设置本指令的 IRName 为结果变量名，确保 getDefVars 可以获取到 %c*
    setIRName(result_->getIRName());

    addOperand(result_);
    addOperand(ptr_);
}

void LoadInstruction::toString(std::string & str)
{
    std::string resultName = result_ ? result_->getIRName() : "null";
    std::string typeName = result_ ? result_->getType()->toString() : "null";
    std::string ptrType = ptr_ ? ptr_->getType()->toString() : "null";
    std::string ptrName = ptr_ ? ptr_->getIRName() : "null";
    str = resultName + " = load " + typeName + ", " + ptr_->getType()->toString() + " " + ptrName;
}

// std::vector<std::string> LoadInstruction::getDefVars()
// {
//     std::vector<std::string> defVars;
//     if (result_ && !result_->getIRName().empty()) {
//         const std::string & name = result_->getIRName();
//         std::cout << "Load result IRName: " << name << std::endl;
//         if (!name.empty() && name[0] != '.' && name.find(".L") != 0) {
//             defVars.push_back(name);
//         }
//     }
//     return defVars;
// }

// std::vector<std::string> LoadInstruction::getUseVars()
// {
//     std::vector<std::string> useVars;
//     if (ptr_ && !ptr_->isConst()) {
//         const std::string & name = ptr_->getIRName();
//         std::cout << "Load ptr IRName: " << name << std::endl;
//         if (!name.empty() && name[0] != '.' && name.find(".L") != 0) {
//             useVars.push_back(name);
//         }
//     }
//     return useVars;
// }